package com.project.controller;
import com.project.service.UserService;
import com.project.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Date;
//@RequestMapping(value = "/hello")
@Controller
public class UserController {
    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/index.jsp")//之前写的是index.html
//    public String loginPage(){
////        return new ModelAndView("login");
//        return "login";
//    }

    @RequestMapping(value = "/loginCheck")//loginCheck.html
    public ModelAndView loginCheck(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
//        boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassword());
//        System.out.println("尝试");
        boolean isValidUser = userService.hasMatchUser(request.getParameter("username"), request.getParameter("password"));
        if (!isValidUser){
            return new ModelAndView("login","error","用户名或密码错误");
        }else{
//            User user = userService.findUserByUserName(loginCommand.getUsername());
            User user = userService.findUserByUserName(request.getParameter("username"));
            //System.out.println("loginCheck："+user.getCredits());
            System.out.println(user.toString());
//            userService.loginSuccess(user);
            request.getSession().setAttribute("user",user);
            return new ModelAndView("main");
        }
    }
}
